<template>
  <div class="news-list">
    <!-- 顶部组件 -->
    <My-Header header_title="新闻资讯"></My-Header>
    <!-- 遍历渲染：新闻资讯列表 -->
    <ul>
      <li v-for="news in newsList" :key="news.id" @click="jumpTo(news.id)">
        <div class="news-item">
          <img :src="news.img_url" alt="news.title">
          <div class="content">
            <p class="content-title">{{news.title}}</p>
            <p class="content-info">
              <span class="content-time">{{news.add_time | handleTime}}</span>
              <span class="content-click">点击{{news.click}}次</span>
            </p>
          </div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
// 引入已封装好MyHeader组件
import MyHeader from '@/components/MyHeader'
// 引入封装的路由请求函数
import { getnewslist } from '@/api'

export default {
  // 注入需要使用的组件
  components: {
    MyHeader
  },
  data () {
    return {
      // 新闻资讯列表数组
      newsList: []
    }
  },
  created () {
    /* 初始化表格数据 */
    this.initList()
  },
  methods: {
    /* 初始化列表函数 */
    initList () {
      // 发请求获取数据
      getnewslist().then(res => {
        if (res.status === 0) {
          this.newsList = res.message
        } else {
          alert('获取数据失败')
        }
      })
    },
    /* 路由跳转 */
    jumpTo (id) {
      // 跳转到相应的商品详情页
      this.$router.push('/getnew/' + id)
    }
  }
}
</script>

<style lang="scss" scoped>
.news-list {
  ul {
    padding: 40px 0 0 0;
    li {
      padding: 5px;
    }
  }
  .news-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      width: 100px;
      height: 50px;
      margin-right: 10px;
      flex: 1;
    }
    .content {
      flex: 2;
      width: 250px;
      .content-title {
        font-size: 14px;
        font-weight: bold;
      }
      .content-info {
        display: flex;
        justify-content: space-between;
        color: #26a2ff;
        font-size: 12px;
      }
    }
  }
}
</style>
